package com.synjones.handsetS8.ild.pages;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.synjones.handsetS8.R;;
import com.synjones.handsetS8.ild.net.api.AccountApi;
import com.synjones.handsetS8.ild.net.common.NetCallback;
import com.synjones.handsetS8.ild.net.common.NetResponse;
import com.synjones.handsetS8.ild.net.model.LoginResult;
import com.synjones.handsetS8.ild.net.model.NetResult;
import com.synjones.handsetS8.ild.utils.SPUtils;

public class LoginActivity extends AppCompatActivity {
    private EditText etAccount, etPassword;
    private Button btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_login);

        initView();
    }

    private void initView() {
        etAccount = (EditText) findViewById(R.id.et_account);
        etPassword = (EditText) findViewById(R.id.et_password);
        btnLogin = (Button) findViewById(R.id.btn_login);

        //测试
        etAccount.setText("18600000001");
        etPassword.setText("123456");

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == etAccount.getText() || "".equals(etAccount.getText().toString().trim())) {
                    Toast.makeText(LoginActivity.this, "请输入登录账号或者手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (null == etPassword.getText() || "".equals(etPassword.getText().toString().trim())) {
                    Toast.makeText(LoginActivity.this, "请输入登录密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                String account = etAccount.getText().toString().trim();
                String password = etPassword.getText().toString().trim();
                AccountApi.login(account, password, new NetCallback<NetResult<LoginResult>>() {
                    @Override
                    protected void onComplete(NetResponse<NetResult<LoginResult>> netResponse) {
                        if (netResponse.isSuccess && null != netResponse.data) {
                            String accessToken = netResponse.data.getData().getAccess_token();
                            String refreshToken = netResponse.data.getData().getRefresh_token();
                            SPUtils.save("accessToken", accessToken);
                            SPUtils.save("refreshToken", refreshToken);
                            System.out.println("AccessToken: " + accessToken);
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                            finish();
                        }
                    }
                });
            }
        });
    }
}